package alpaca.view;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import alpaca.core.IUser;
import alpaca.core.MessageList;
import alpaca.core.UserList;
import alpaca.util.BiDirMap;
import alpaca.util.DBConfigData;
import alpaca.util.SystemMessage;
import alpaca.view.panels.ErrorDialog;
import alpaca.view.panels.LogPanel;
import alpaca.view.panels.ShowLogFrame;
import alpaca.view.panels.StatusPanel;
import alpaca.view.panels.UserPanel;

public class ServerView extends JFrame implements PropertyChangeListener{

	private static final String LOG_TOOLTIP = "Shows logs of all user chat activity";
	private static final String USER_TOOLTIP = "Allows you to edit the users saved in the database";
	private static final String STATUS_TOOLTIP = "Shows general status of the server and the ability to toggle it on and off";
	private static final String SERVER_STATUS_TAB = "Server Status";
	private static final String EDIT_USERS_TAB = "Edit Users";
	private static final String LOG_TAB = "Logs";
	private static final String ALPACA_SERVER = "Alpaca Server";
	
	private JPanel contentPane;
	private StatusPanel statusPanel;
	private LogPanel logPanel;
	private UserPanel userPanel;
	private JTabbedPane tabbedPane;

	/**
	 * Create the frame.
	 */
	public ServerView() {
		setTitle(ALPACA_SERVER);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setFont(new Font("Tahoma", Font.PLAIN, 12));
		setBounds(100, 100, 559, 458);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
		
		tabbedPane = new JTabbedPane(SwingConstants.TOP);
		tabbedPane.setBorder(null);
		contentPane.add(tabbedPane);
		
		statusPanel = 	new StatusPanel();
		tabbedPane.addTab(SERVER_STATUS_TAB, null, statusPanel, null);

		
		userPanel = new UserPanel();
		tabbedPane.addTab(EDIT_USERS_TAB, null, userPanel, null);
		
		logPanel = new LogPanel();
		tabbedPane.addTab(LOG_TAB, null, logPanel, null);
		

		tabbedPane.setEnabledAt(1, false);
		tabbedPane.setEnabledAt(2, false);
		
		tabbedPane.setToolTipTextAt(0, STATUS_TOOLTIP);
		tabbedPane.setToolTipTextAt(1, USER_TOOLTIP);
		tabbedPane.setToolTipTextAt(2, LOG_TOOLTIP);

		tabbedPane.setDisplayedMnemonicIndexAt(0, 0);
		tabbedPane.setDisplayedMnemonicIndexAt(1, 0);
		tabbedPane.setDisplayedMnemonicIndexAt(2, 0);
		
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_S);
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_E);
		tabbedPane.setMnemonicAt(2, KeyEvent.VK_L);

		
		setVisible(true);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getNewValue() instanceof SystemMessage){
			SystemMessage sm = (SystemMessage) evt.getNewValue();
			switch (sm.getType()){

			case ERROR_MESSAGE: 
				String error = (String) sm.getObject();

				new ErrorDialog(error, getLocation()).setVisible(true);
				break;
			
			case CONN_AMOUNT: 
				int connections = (Integer) sm.getObject();
				statusPanel.setConnectedText(connections+"");
				break;

			case SERV_STATUS:
				boolean serverState = (Boolean) sm.getObject();
				statusPanel.setStatus(serverState);
				if (!serverState){
					tabbedPane.setSelectedIndex(0); 
					tabbedPane.setEnabledAt(1, false);
					tabbedPane.setEnabledAt(2, false);
					userPanel.clearUserList();
				}else {
					tabbedPane.setEnabledAt(1, true);
					tabbedPane.setEnabledAt(2, true);
				}
				break;
			case UPDATE_CONN: 
				BiDirMap<IUser, Integer> userMapping = (BiDirMap<IUser, Integer>) sm.getObject();
				statusPanel.addUserList(userMapping.keySet());
				statusPanel.setConnectedText(userMapping.keySet().size()+"");
				break;
			case UPDATE_USERS:
				UserList userList = (UserList) sm.getObject();
				userPanel.fillUserList(userList);
				logPanel.fillUserList(userList);
				break;
			case UPDATE_DB_CONFIG:
				statusPanel.setDBConfigData((DBConfigData)sm.getObject());
				break;
			case MESSAGE_LOG:
				MessageList ml = (MessageList) sm.getObject();
				new ShowLogFrame(ml);
				break;
			}
		} 	
		
	}
	

}
